package login.action;

import account.entity.AccountVO;
import account.service.AccountService;
import com.opensymphony.xwork2.ActionSupport;
import login.service.LoginService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import util.ObtainOther;

import javax.servlet.http.HttpSession;

/**
 * 关于登录的操作
 */
public class loginAction extends ActionSupport {

    private final static Log logger = LogFactory.getLog(loginAction.class);


    private AccountService accountService;

    public AccountService getAccountService() {
        return accountService;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    private LoginService loginService;

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    private AccountVO accountVO;

    public AccountVO getAccountVO() {
        return accountVO;
    }

    public void setAccountVO(AccountVO accountVO) {
        this.accountVO = accountVO;
    }

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        message = message;
    }

    public String login() {
        try{
            accountVO=loginService.validateLogin(accountVO);
            if (accountVO == null) {
                message= ObtainOther.obtainMessage("loginFail");
                return INPUT;
            } else {
                HttpSession session = ServletActionContext.getRequest().getSession();
                session.setAttribute("accountVO", accountVO);
                return SUCCESS;
            }
        }catch (Exception e){
            message= ObtainOther.obtainMessage("loginError");
            logger.error(message + ":/n" + e);
            return ERROR;
        }

    }
    public String logout(){
        try{
            HttpSession session =ServletActionContext.getRequest().getSession();
            session.removeAttribute("accountVO");
            return SUCCESS;
        }catch (Exception e){
            message= ObtainOther.obtainMessage("loginError");
            logger.error(message + ":/n" + e);
            return ERROR;
        }

    }


}




